<?php 

require_once('../../../conexiones/cn.php');

$busqueda = $_GET['p'];
$resultado = array();

function JEncode($arr){
    if (version_compare(PHP_VERSION,"5.2","<"))
    {    
        require_once("../../../includes/JSONs.php");   //if php<5.2 need JSON class
        $json = new Services_JSON();  //instantiate new json object
        $data=$json->encode($arr);    //encode the data in json format
    } else
    {
        $data = json_encode($arr);    //encode the data in json format
    }
    return $data;
}

	$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
	$query_rs = sprintf("SELECT titulo_libro as tokens FROM libro 
									WHERE titulo_libro ILIKE '%%%s%%'
								", $busqueda);
	$rs = $cn->Execute($query_rs) or die($cn->ErrorMsg());
	$totalRows_rs = $rs->RecordCount();

	
	$i = 0;
	$arr = array();

	while($row = $rs->FetchRow()) { 
		$arr[] = $row;
	}

	$jsonres = JEncode($arr);
	//echo $jsonres;
	$rs->Close();

// validaciones
if (is_string($busqueda) && strlen($busqueda) > 2 && strlen($busqueda) < 64)
{
/*
    $dbh = new PDO('postgres:host=localhost;dbname=sagi_1_8', 'sigbi', 'sigbi');
 
    // consulta
    $stmt = $dbh->prepare("SELECT titulo_libro FROM libro WHERE titulo_libro ILIKE ?");
 
    // The % as wildcard
    if ($stmt->execute(array($busqueda . '%') ) )
    {
        // Filling the results with usernames
        while (($row = $stmt->fetch() ) )
        {
            $resultado[] = $row['titulo_libro'];
        }
    }
	*/
}

//header('Content-type: application/json');
 
echo json_encode($arr); 


?>